<template>
  <div>
    <div style="margin: 10px 0">
      <el-input
        style="width: 200px"
        placeholder="请输入名称"
        v-model="username"
        suffix-icon="avatar"
      ></el-input>

      <el-input
        v-model="email"
        style="width: 200px"
        class="ml-5"
        placeholder="请输入邮箱"
        suffix-icon="message"
      ></el-input>
      <el-input
        style="width: 200px"
        placeholder="请输入电话"
        v-model="phone"
        suffix-icon="iphone"
      ></el-input>
      <el-input
        v-model="address"
        style="width: 200px"
        class="ml-5"
        placeholder="请输入地址"
        suffix-icon="home-filled"
      ></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>
    <div style="margin: 10px 0; display: inline-flex">
      <el-button type="primary" icon="circle-plus" @click="handleAdd"
        >新增</el-button
      >
      <el-popconfirm
        width="200px"
        confirmButtonText="确定"
        cancelButtonText="取消"
        icon="check"
        iconColor="red"
        title="您确定批量删除吗？"
        @confirm="delBatch()"
      >
        <template #reference>
          <el-button type="danger" icon="remove">批量删除</el-button>
        </template>
      </el-popconfirm>

      <el-upload
        :show-file-list="false"
        accept="xlsx"
        :on-success="handleExcelImportSuccess"
        action="http://localhost:9090/user/import/"
        :limit="1"
        style="display: inline-block"
      >
        <el-button type="primary" icon="bottom" class="ml-5">导入</el-button>
      </el-upload>

      <el-button type="primary" icon="top" @click="exp" class="ml-5"
        >导出</el-button
      >
    </div>
    <el-table
      :data="tableData"
      border
      stripe
      header-cell-class-name="headerBg"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="ID" width="80"> </el-table-column>
      <el-table-column prop="username" label="用户名" width="150">
      </el-table-column>
      <el-table-column prop="role" label="角色" width="150"> </el-table-column>
      <el-table-column prop="nickname" label="昵称" width="150">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="250"> </el-table-column>
      <el-table-column prop="phone" label="电话" width="250"> </el-table-column>
      <el-table-column prop="address" label="地址" width="250">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="success" icon="edit" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="确定"
            cancelButtonText="取消"
            icon="check"
            iconColor="red"
            title="您确定删除吗？"
            @confirm="del(scope.row.id)"
          >
            <template #reference>
              <el-button type="danger" icon="delete">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <div>
      <el-dialog title="用户信息:" v-model="dialogFormVisible" width="22%">
        <el-form :model="form" label-width="80px" size="small">
          <el-form-item label="用户名:">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色:">
            <el-select
              clearable
              v-model="form.role"
              placeholder="请选择角色"
              style="width: 100%"
            >
              <el-option
                v-for="item in roles"
                :key="item.name"
                :label="item.name"
                :value="item.flag"
              ></el-option
            ></el-select>
          </el-form-item>
          <el-form-item label="昵称:">
            <el-input v-model="form.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱:">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="手机号码:">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址:">
            <el-input v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="randomTb" style="margin-right: 150px"
              >随 机</el-button
            >
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
export default {
  name: "User",
  data() {
    const item = {
      date: "2016-05-02",
      name: "王小虎",
      address: "上海市普陀区金沙江路 1518 弄",
    };
    return {
      //tableData: Array(10).fill(item),
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      username: "",
      nickname: "",
      phone: "",
      email: "",
      address: "",
      msg: "青哥哥",
      collapseBtnClass: "fold",
      isCollapse: false,
      sideWidth: 200,
      myIcon: "fold",
      logoTextShow: true,
      form: [],
      dialogFormVisible: false,
      multipleTable: [],
      roles: [],
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request
        .get("/user/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            username: this.username,
            email: this.email,
            phone: this.phone,
            address: this.address,
          },
        })
        .then((response) => {
          //console.log(response);
          this.tableData = response.data.records;
          this.total = response.data.total;
        });

      this.request.get("/role").then((response) => {
        this.roles = response.data;
      });

      // fetch(
      //   "http://localhost:9090/user/page?pageNum=" +
      //     this.pageNum +
      //     "&pageSize=" +
      //     this.pageSize +
      //     "&username=" +
      //     this.username +
      //     "&email=" +
      //     this.email +
      //     "&address=" +
      //     this.address
      // )
      //   .then((response) => response.json())
      //   .then((json) => {
      //     console.log(json);
      //     this.total = json.total;
      //     this.tableData = json.data;
      //   });
    },
    reset() {
      this.username = "";
      this.email = "";
      this.phone = "";
      this.address = "";
      this.load();
    },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum;
      this.load();
    },
    handleAdd() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    randomTb() {
      this.form.username = Math.random().toString(36).substring(2);
      this.form.nickname = Math.random().toString(36).substring(2);
      this.form.email = Math.random().toString(36).substring(2);
      this.form.phone = Math.random().toString(36).substring(2);
      this.form.address = Math.random().toString(36).substring(2);
      console.log(this.form);
    },
    save() {
      this.request.post("/user", this.form).then((response) => {
        if (response.code === "200") {
          this.$message.success("添加成功");
          this.dialogFormVisible = false;
          this.load();
        } else {
          this.$message.error("添加失败");
        }
        console.log(response.data);
      });
    },
    handleEdit(row) {
      this.form = row;
      this.dialogFormVisible = true;
    },
    del(id) {
      this.request.delete("/user/" + id, this.form).then((response) => {
        if (response.code === "200") {
          this.$message.success("删除成功"); //顶部弹出提示框。
          this.dialogFormVisible = false;
          this.load();
        } else {
          this.$message.error("删除失败");
        }
        console.log(response.data);
      });
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleTable = val;
    },
    delBatch() {
      //把对象数组。变成纯ID数组。
      let ids = this.multipleTable.map((item) => item.id); //[{},{},{}] =>  [1,2,3]
      this.request.post("/user/del/batch", ids).then((response) => {
        if (response.code === "200") {
          this.$message.success("批量删除成功"); //顶部弹出提示框。
          //this.dialogFormVisible = false;
          this.load();
        } else {
          this.$message.error("批量删除失败");
        }
        console.log(response.data);
      });
    },
    exp() {
      window.open("http://localhost:9090/user/export");
    },
    handleExcelImportSuccess() {
      this.$message.success("文件导入成功");
      this.load(); //刷新一下
    },
  },
};
</script>
<style scoped>
.headerBg {
  background: #ccc !important;
}
</style>
